﻿using Hxj.Data;
using RecruitStudents.Utility.Helper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RecruitStudents.Client.DAL.Base
{
    public class BaseService
    {
        public TResult ToScalar<TResult>(string strSql)
        {
            return DbHelper.GetDatabase().FromSql(strSql).ToScalar<TResult>();
        }

        public IDataReader GetDataReader(string strSql)
        {
            return DbHelper.GetDatabase().FromSql(strSql).ToDataReader();
        }

        public DataTable GetTable(string strSql)
        {
            return DbHelper.GetDatabase().FromSql(strSql).ToDataTable();
        }

        public DataSet GetDataSet(string strSql)
        {
            return DbHelper.GetDatabase().FromSql(strSql).ToDataSet();
        }

        public void ExecuteNonQuery(string strSql)
        {
            DbHelper.GetDatabase().FromSql(strSql).ExecuteNonQuery();
        }

        public void ExecuteNonQuery(string strSql, DbTrans trans)
        {
            DbHelper.GetDatabase().FromSql(strSql).SetDbTransaction(trans).ExecuteNonQuery();
        }

        public TResult ToScalar<TEntity, TResult>(WhereClip wcp) where TEntity : Entity
        {
            return DbHelper.GetDatabase().From<TEntity>().Where(wcp).ToScalar<TResult>();
        }

        public int ToCount<TEntity>(WhereClip wcp) where TEntity : Entity
        {
            return DbHelper.GetDatabase().From<TEntity>().Where(wcp).Count();
        }

        public List<TEntity> GetList<TEntity>(WhereClip wcp) where TEntity : Entity
        {
            return DbHelper.GetDatabase().From<TEntity>().Where(wcp).ToList();
        }

        public List<TEntity> GetList<TEntity>(WhereClip wcp, OrderByClip orderby) where TEntity : Entity
        {
            return DbHelper.GetDatabase().From<TEntity>().Where(wcp).OrderBy(orderby).ToList();
        }

        private string sql;
        void database_OnLog(string logMsg)
        {
            //保存执行的DbCommand (sql语句和参数)
            sql += "<br />" + logMsg;
        }

        public DataTable GetTable<TEntity>(WhereClip wcp) where TEntity : Entity
        {
            //DbHelper.GetDatabase().RegisterSqlLogger(database_OnLog);
            DataTable dt = DbHelper.GetDatabase().From<TEntity>().Where(wcp).ToDataTable();
            //DbHelper.GetDatabase().UnregisterSqlLogger(database_OnLog);
            return dt;
        }

        public DataSet GetDataSet<TEntity>(WhereClip wcp) where TEntity : Entity
        {
            return DbHelper.GetDatabase().From<TEntity>().Where(wcp).ToDataSet();
        }

        public DataTable GetTable<TEntity>(WhereClip wcp, OrderByClip orderby) where TEntity : Entity
        {
            return DbHelper.GetDatabase().From<TEntity>().Where(wcp).OrderBy(orderby).ToDataTable();
        }

        public TEntity GetModel<TEntity>(WhereClip wcp) where TEntity : Entity
        {
            TEntity model = DbHelper.GetDatabase().From<TEntity>().Where(wcp).ToFirst();
            if (model != null)
            {
                model.Attach();
            }
            return model;
        }

        public TEntity GetModel<TEntity>(WhereClip wcp, OrderByClip orderby) where TEntity : Entity
        {
            TEntity model = DbHelper.GetDatabase().From<TEntity>().Top(1).Where(wcp).OrderBy(orderby).ToFirst();
            if (model != null)
            {
                model.Attach();
            }
            return model;
        }

        public DataTable GetListPageTable<TEntity>(PagerCondition pager) where TEntity : Entity, new()
        {
            TEntity item = new TEntity();
            pager.RecordCount = DbHelper.GetDatabase().From<TEntity>().Where(pager.ComplexSearchCondition).Count();
            #region 固定写法，判断当前页面对象删除后，当前页和最大页之间的关系
            if (pager.CurrentPage > pager.PageCount)
                pager.CurrentPage = pager.PageCount;
            #endregion
            if (pager.OrderBy == null)
            {
                return DbHelper.GetDatabase().From<TEntity>() //选择表   
                                             .Select(item.GetFields())  //设置选择的字段数目
                                            .Page(pager.PageSize, pager.CurrentPage)  //分页显示
                                            .Where(pager.ComplexSearchCondition)   //条件
                                            .ToDataTable();   //返回datatable 
            }
            else
            {
                return DbHelper.GetDatabase().From<TEntity>() //选择表   
                                             .Select(item.GetFields())  //设置选择的字段数目
                                            .Page(pager.PageSize, pager.CurrentPage)  //分页显示
                                            .Where(pager.ComplexSearchCondition)   //条件
                                            .OrderBy(pager.OrderBy)   //排序
                                            .ToDataTable();   //返回datatable
            }
        }

        public List<TEntity> GetListPage<TEntity>(PagerCondition pager) where TEntity : Entity, new()
        {
            TEntity item = new TEntity();
            pager.RecordCount = DbHelper.GetDatabase().From<TEntity>().Where(pager.ComplexSearchCondition).Count();
            #region 固定写法，判断当前页面对象删除后，当前页和最大页之间的关系
            if (pager.CurrentPage > pager.PageCount)
                pager.CurrentPage = pager.PageCount;
            #endregion
            if (pager.OrderBy == null)
            {
                return DbHelper.GetDatabase().From<TEntity>() //选择表   
                                             .Select(item.GetFields())  //设置选择的字段数目
                                            .Page(pager.PageSize, pager.CurrentPage)  //分页显示
                                            .Where(pager.ComplexSearchCondition)   //条件
                                            .ToList();   //返回datatable 
            }
            else
            {
                return DbHelper.GetDatabase().From<TEntity>() //选择表   
                                             .Select(item.GetFields())  //设置选择的字段数目
                                            .Page(pager.PageSize, pager.CurrentPage)  //分页显示
                                            .Where(pager.ComplexSearchCondition)   //条件
                                            .OrderBy(pager.OrderBy)   //排序
                                            .ToList();   //返回datatable
            }
        }

        public void Save<TEntity>(TEntity attachment, DbTrans trans) where TEntity : Entity
        {
            if (!attachment.IsNewModel)
            {
                //存在则更新
                DbHelper.GetDatabase().Update<TEntity>(attachment, trans);
            }
            else
            {
                //不存在则新增
                DbHelper.GetDatabase().Insert<TEntity>(attachment, trans);
            }
        }

        public bool Save<TEntity>(TEntity attachment) where TEntity : Entity
        {
            DbTrans trans = DbHelper.GetDatabase().BeginTransaction();
            try
            {
                if (!attachment.IsNewModel)
                {
                    //存在则更新
                    DbHelper.GetDatabase().Update<TEntity>(attachment, trans);
                }
                else
                {
                    //不存在则新增
                    DbHelper.GetDatabase().Insert<TEntity>(attachment, trans);
                }
                trans.Commit();
                return true;
            }
            catch (Exception ex)
            {
                trans.Rollback();
                throw ex;
            }
            finally
            {
                trans.Close();
            }
        }

        public void Delete<TEntity>(WhereClip wcp, DbTrans trans) where TEntity : Entity
        {
            DbHelper.GetDatabase().Delete<TEntity>(wcp, trans);
        }
        public bool Delete<TEntity>(WhereClip wcp) where TEntity : Entity
        {
            DbTrans trans = DbHelper.GetDatabase().BeginTransaction();
            try
            {
                DbHelper.GetDatabase().Delete<TEntity>(wcp, trans);
                trans.Commit();
                return true;
            }
            catch (Exception ex)
            {
                trans.Rollback();
                throw ex;
            }
            finally
            {
                trans.Close();
            }
        }
    }
}
